package com.ssh.cp.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;

import com.ssh.cp.entity.LoginLog;
import com.ssh.cp.idao.ILogDao;

@Repository 
public class LogDao implements ILogDao{
    @Resource(name="sessionFactory")   
    private SessionFactory sessionFactory;  
	@Override
	public void addLoginLog(LoginLog loginlog) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.openSession();
        
        try {
            session.save(loginlog);  
        }catch(RuntimeException re) {			
			throw re;
		}finally {
			session.close();
		}
	}
	@Override
	public List<?> listLoginLog(int start,int length) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.openSession();
		List<?> list=null;
		Query<?> query=null;
 		try {
		query=session.createQuery("from loginLog");
		  
		  query.setFirstResult(start);
          query.setMaxResults(length);
          list=query.list();
          }catch(RuntimeException re) {			
			throw re;
		}finally {
			session.close();
		}
		
		
		return list;
	}
	@Override
	public int getLoginLogNum() {
		// TODO Auto-generated method stub
		Session session = sessionFactory.openSession();
		int i=0;
		Query<?> query=null;
 		try {
		query=session.createQuery("from loginLog");
		  
		
          i=query.list().size();
          }catch(RuntimeException re) {			
			throw re;
		}finally {
			session.close();
		}
		
		
		return i;
	}

	
}
